#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int ans = 0;
        for (int i = 0, j = 0; j < prices.size();)
        {
            while (j + 1 < prices.size() && prices[j + 1] >= prices[j]) j++;
            {
                ans += prices[j] - prices[i];
                i = j + 1;
                j = i;
            }
        }
        return ans;
    }
};